package com.yzl.shiro.demo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * 单元侧测试执行用例
 *
 * @BeforeClass->@Before->@Test->@Test->@AfterClass
 */
public class QuickStartTest5_4 {
    private CustomRealm customRealm = new CustomRealm();
    private DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();


    @Before
    public void init() {
        //构建环境
        defaultSecurityManager.setRealm(customRealm);
        SecurityUtils.setSecurityManager(defaultSecurityManager);
    }

    @Test
    public void testAuthentication() {
        //获取当前操作主体
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack", "123");
        subject.login(usernamePasswordToken);
        System.out.println("认证结果：" + subject.isAuthenticated());
        //拿到主体标识属性
        System.out.println("getPrinciple:" + subject.getPrincipal());

        subject.checkRole("role1");
        System.out.println("是否有对应的角色："+subject.hasRole("role1"));
        System.out.println("是否有对应得权限："+subject.isPermitted("video:buy"));
    }
}
